package com.css.msgcenter.admin.msgapp.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.dict.service.DictMan;
import com.css.core.model.IServiceResult;
import com.css.core.model.ModeFactory;
import com.css.db.query.TransactionCache;
import com.css.msgcenter.server.QueryMsg;
import com.css.msgcenter.server.model.MsgApp;
import com.css.msgcenter.server.model.MsgAppIp;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.util.UuidUtil;

public class MsgAppService {
	private static Log log = LogFactory.getLog(MsgAppService.class);
	
	public static String getMsgTypes(String types) {
		if(StringHelper.isEmpty(types))
			return "";
		StringBuilder sb = new StringBuilder();
		String[] arr = types.split(",");
		if(arr != null) {
			for(int i=0; i<arr.length; i++) {
				sb.append("[").append(DictMan.getDictType("msg_d_msgtype",arr[i]).getItemName()).append("] ");
			}
		}
		return sb.toString();
	}
	
	public static IServiceResult<?> addMsgApp(MsgApp msgApp,String ips) {
		IServiceResult<?> res = null;
		String result = "";
		TransactionCache tx = null;
		if(msgApp==null){
			res = ModeFactory.getModeFactory().buildNewServiceResult(
					IServiceResult.RESULT_FAILED,
					Messages.getString("systemMsg.fieldEmpty"));
			return res;
		}
		try{
			Object uuid = new QueryMsg("select a.uuid from MsgApp a where a.appCode=:appCode")
			.setParameter("appCode",msgApp.getAppCode()).setMaxResults(1).uniqueResult();
			if(uuid != null){
				res=ModeFactory.getModeFactory().buildNewServiceResult(
						IServiceResult.RESULT_FAILED, 
						Messages.getString("systemMsg.appCodeExist"), result);
				return res;
			}
			
			tx = new QueryMsg().getTransaction();
			tx.save(msgApp);
			//ip表
			if (!StringHelper.isEmpty(ips)) {
				List<String> msgAppIpList = StringHelper.strToList(ips);
				for(int i=0; i<msgAppIpList.size(); i++){
					MsgAppIp msgAppIp =new MsgAppIp();
					msgAppIp.setUuid(UuidUtil.getUuid());
					msgAppIp.setAppId(msgApp.getUuid());
					msgAppIp.setAppSysIp(msgAppIpList.get(i).trim());
					tx.save(msgAppIp);
				}
			}
			tx.commit();
			res = ModeFactory.getModeFactory().buildNewServiceResult(
				IServiceResult.RESULT_OK,
				Messages.getString("systemMsg.success"), result);
			return res;
		}catch(Exception ex){
			log.error(ex.getMessage(), ex);
			if (tx != null)tx.rollback();
			ex.printStackTrace();
			res = ModeFactory.getModeFactory().buildNewServiceResult(
				IServiceResult.RESULT_FAILED, Messages.getString("systemMsg.dbFaild"), result);
			return res;
		}
	}
	
	@SuppressWarnings("unchecked")
	public static IServiceResult<?> delMsgApp(String ids) {
		IServiceResult<?> res = null;
		TransactionCache tx = null;
		String result = "";
		if (StringHelper.isEmpty(ids)) {
			res = ModeFactory.getModeFactory().buildNewServiceResult(
					IServiceResult.RESULT_FAILED,
					Messages.getString("systemMsg.dbFaild"));
			return res;
		}
		try{
			List<String> msgAppIdList = StringHelper.strToList(ids);
			java.util.List<MsgApp> msgAppList = new QueryMsg().idToObj(MsgApp.class, msgAppIdList);
			tx = new QueryMsg().getTransaction();
			for(MsgApp ls: msgAppList){
				//此处应有关联删除
/*				QueryMsg qm = new QueryMsg("select a.uuid from MsgAppIp a where a.appId = :appId");
				qm.setParameter("appId", ls.getUuid());
				List<MsgAppIp> msgAppIpList = QueryMsg.idToObj(MsgAppIp.class,qm.listCache());
				for(MsgAppIp ipList: msgAppIpList){
					tx.delete(ipList);
				}*/
				QueryMsg qm = new QueryMsg("select a.uuid from MsgAppIp a where a.appId = :appId");
				List list = qm.setParameter("appId", ls.getUuid()).list();
				tx.delete(QueryMsg.idToObj(MsgAppIp.class, list));
				tx.delete(ls);
			}
			tx.commit();
			res = ModeFactory.getModeFactory().buildNewServiceResult(
					IServiceResult.RESULT_OK,
					Messages.getString("systemMsg.success"),result);
			return res;
		}catch(Exception ex){
			log.error(ex.getMessage(), ex);
			if (tx != null){tx.rollback();}
			ex.printStackTrace();
			res = ModeFactory.getModeFactory().buildNewServiceResult(
					IServiceResult.RESULT_FAILED,
					Messages.getString("systemMsg.dbFaild"),result);
			return res;
		}

	}
	
	public static IServiceResult<?> updMsgApp(String ids,String openStatus) {
		IServiceResult<?> res = null;
		TransactionCache tx = null;
		String result = "";
		List lst = StringHelper.strToList(ids);
		try{
			List<MsgApp> msgAppLst = (List<MsgApp>) QueryMsg.idToObj(MsgApp.class, lst);
			tx = new QueryMsg().getTransaction();
			for(MsgApp msgApp : msgAppLst ){
				msgApp.setOpenFlag(openStatus);
				tx.update(msgApp);
			}
			tx.commit();
			res = ModeFactory.getModeFactory().buildNewServiceResult(
					IServiceResult.RESULT_OK,
					Messages.getString("systemMsg.success"),result);
			return res;
		}catch(Exception ex){
			log.error(ex.getMessage(), ex);
			if (tx != null){tx.rollback();}
			ex.printStackTrace();
			res = ModeFactory.getModeFactory().buildNewServiceResult(
					IServiceResult.RESULT_FAILED,
					Messages.getString("systemMsg.dbFaild"),result);
			return res;
		}
	}
	
	
	public static IServiceResult<?> updMsgApp(MsgApp msgApp,String ips) {
		IServiceResult<?> res = null;
		TransactionCache tx = null;
		String result = "";
		List newList = StringHelper.strToList(ips);
		try{
			tx = new QueryMsg().getTransaction();
			QueryMsg qm = new QueryMsg("select a.uuid from MsgAppIp a where a.appId = :appId");
			List oldList = qm.setParameter("appId", msgApp.getUuid()).list();
			tx.delete(QueryMsg.idToObj(MsgAppIp.class, oldList));
			tx.update(msgApp);
			if (!StringHelper.isEmpty(ips)) {
				List<String> msgAppIpList = StringHelper.strToList(ips);
				for(int i=0; i<msgAppIpList.size(); i++){
					MsgAppIp msgAppIp =new MsgAppIp();
					msgAppIp.setUuid(UuidUtil.getUuid());
					msgAppIp.setAppId(msgApp.getUuid());
					msgAppIp.setAppSysIp(msgAppIpList.get(i).trim());
					tx.save(msgAppIp);
				}
			}
			msgApp.getIpList().removeAll();
			tx.commit();
			res = ModeFactory.getModeFactory().buildNewServiceResult(
					IServiceResult.RESULT_OK,
					Messages.getString("systemMsg.success"),result);
			return res;
		}catch(Exception ex){
			log.error(ex.getMessage(), ex);
			if (tx != null)tx.rollback();
			ex.printStackTrace();
			res = ModeFactory.getModeFactory().buildNewServiceResult(
					IServiceResult.RESULT_FAILED, Messages.getString("systemMsg.dbFaild"),result);
			return res;
		}
	}
}
